home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: New printf - like function, is it possible?
- Date: Thu, 01 Feb 1996 14:40:24 +0200
- Organization: Carelcomp Forest
- Message-ID: <3110B4B8.5634@cmt.lpr.mail.carel.fi>
- References: <4eqb3a$2r5@pan.otol.fi>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
-
- Timo Sakari wrote:
- >
- > Hi!
- >
- > Do you C-gurus have any solutions to this problem?
- >
- > I am programming under MS Windows 3.1 (this is NOT an OS spesific
- > question!)and I would like to create
- > some kind of modified print function, which syntax should be something
- > like normal printf; because in Windows printf can't be used, everything
- > printed to screen has to come through message boxes etc, and before
- > displaying anything, the string to displayed has to be formed. I have
- > created my own printing function, which wants parameters string and winID
- > to know where to put that string, something like this:
- >
- > sprintf(printStr, "some sfuff, variables %d, %f, etc", var1, var2);
- > PrintToWin(DEBUG_WIN_1, printStr);
- >
- > However, I want to write a "better" printing function, where string to be
- > formed doesn't need to be formatted first, it can be displayed and formed
- > at the same time in the same syntax like printf, something like these:
- >
- > PrintToWin(DEBUG_WIN_1, "some stuff etc, %d, %f, %s, var1, var2, str1);
- > PrintToWin(OTHER_WIN, "anything that printf accepts");
- >
- > So I want to print almost anything with this function without having
- > to write many functions to different argument types/number of arguments.
- > This should be possible in C++ (is it?), but is it possible in C ?
- > Can this be achieved by using void pointers and macros etc., or should
- > I give up and don't waste my time on this?
- >
- > Any help would be greatly appreciated. Thanks.
-
- You could try:
-
- #include <stdarg.h>
-
- void MyPrintf(HDC hDC, int x, int y, char *fmt, ...)
- {
- va_list argp;
- char tmp[long_enough_for_your_needs];
-
- va_start(argp, fmt);
- vsprintf(tmp, fmt, argp);
- TextOut(hDC, x, y, tmp, strlen(tmp));
- va_end(argp);
- }
-
- Alternatively, if you'd want TTY like output, you can GetTextMetrics for the font you use and
- use the metrics to increment x and y as required (taking into account newlines etc) so you
- won't have to supply them as parameters each time.
-
- HTH,
- AriL
- --
- All my opinions are mine and mine alone.
-